package com.qf.controller;

import com.qf.entity.SysUser;
import com.qf.service.IUserService;
import com.qf.utils.security.JwtUtils;
import com.qf.utils.security.RsaUtils;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;

/**
 * <p>title: com.qf.controller</p>
 * <p>Company: wendao</p>
 * author zhuximing
 * date 2021/9/23
 * description:
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("login")
    public Result login(@RequestBody SysUser user){

        return  userService.login(user);
    }


    @RequestMapping("verify")
    public Result verify(@RequestHeader("token") String token){

        if(StringUtils.isEmpty(token)){

            return  new Result<>(false,"令牌不能为空");
        }

        try {
            PublicKey publicKey = RsaUtils.getPublicKey(ResourceUtils.
                getFile("classpath:rsa_pub").getPath());

            Object infoFromToken = JwtUtils.getInfoFromToken(token, publicKey, SysUser.class);


            return  new Result<>(true  ,"令牌有效");
        } catch (Exception exception) {
            exception.printStackTrace();

            return  new Result<>(false,"无效令牌");
        }

    }


}